#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'arrow'
__explain__ = ''


from moviepy.video.VideoClip import TextClip
from moviepy.video.compositing import CompositeVideoClip
from moviepy.video.compositing.concatenate import concatenate_videoclips


def compose_text(text_list: list, size, font="WenQuanYi-Zen-Hei", font_size=60, text_color=(255, 255, 255),
                 duration=None, duration_speed=6) -> [TextClip]:
    """
    生产字幕剪辑
    :param text_list:
    :param size:
    :param font:
    :param font_size:
    :param text_color:
    :param duration:
    :param duration_speed: 字/秒，根据速度来设置字幕时长
    :return:
    """
    text_clips = []
    size = (size[0], None)   # method='caption' 高度自动判定
    for i in text_list:
        if duration:
            text_duration = duration
        else:
            text_duration = len(i)//duration_speed + 1
        # 设置字幕，字体大小、颜色、字体文件
        text_clips.append(
            TextClip(
                i, fontsize=font_size, color=text_color, font=font, method='caption', size=size
            ).set_duration(text_duration)
        )
    text_clips = concatenate_videoclips(text_clips).set_position(("center", 0.8), relative=True)
    return text_clips
